<?php
/**
 * 结算
 * @author jianhui
 *
 */
class SingleSettle extends MY_Controller{
    function SingleSettle(){
        parent::MY_Controller();
    }
    /**
     * 专营店列表
     */
    function storeList(){
        if ($this->user['userRole'] != 1){
            showError('您无权限进去此页面');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleSettle', 'storeList'),'title'=>'结算申请列表');
        //载入模型
        $this->load->model('singleProcessModel');
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时间单位配置
        $this->load->model('timeUnitModel');
        $this->viewData['timeUnitList'] = $this->timeUnitModel->getTimeUnitList($year);
        $where = array();
        //专营店ID条件
        $where['storeId'] = array('in', $this->userLib->getViewStoreIds());
        //年份条件
        $where['year'] = $year;
        $procList = $this->singleProcessModel->getNewListByTimeUnit($where);
        foreach ($procList as $k => $v){
            $procList[$k]['auditCount'] = $this->singleProcessModel->getSettleStatusCount($v['id'], 0);
        }
        $this->viewData['procList'] = $procList;
        
        $this->load->view('SingleSettle/storeList', $this->viewData);
    }
    /**
     * 结算凭证上传
     */
    function storeUpload(){
        $this->load->model('singleProcessModel');
        if ($_POST){
            $procId = (int)$this->input->post('procId');
            $procId || showError('请选择要上传凭证的双月数据');
            $fileId = (int)$this->input->post('fileId');
            $fileId || showError('您还未上传凭证');
            if ($this->singleProcessModel->edit($procId, array('fileId'=>$fileId))){
                showSuccess('保存结算凭证成功',printUrl('SingleSettle', 'storeEdit', array('procId'=>$procId)));
            }else{
                showError('保存凭证数据出错');
            }
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择要提交结算的双月数据');
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleSettle', 'storeList'),'title'=>'结算申请列表');
            $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'凭证上传');
            //主线流程数据
            $procData = $this->singleProcessModel->getNewData(array('id'=>$procId,'storeId'=>$this->user['userId']));
            $this->viewData['procData'] = $procData;
            if (!$this->auth->isSingleSettleEdit($procData)){
                showError('您无权限申请结算');
            }
            if ($procData['fileId']){
                redirect(printUrl('SingleSettle', 'StoreEdit', array('procId'=>$procId)));
            }
            //操作
            $this->viewData['action'] = 'edit';
            $this->load->view('SingleSettle/storeUpload', $this->viewData);
        }
    }
    /**
     * 结算申请
     */
    function storeEdit(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择要提交结算的双月数据');
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleSettle', 'storeList'),'title'=>'结算申请列表');
        $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'结算申请');
        //主线流程数据
        $this->load->model('singleProcessModel');
        $procData = $this->singleProcessModel->getNewData(array('id'=>$procId,'storeId'=>$this->user['userId']));
        $this->viewData['procData'] = $procData;
        if (!$this->auth->isSingleSettleEdit($procData)){
            showError('您无权限申请结算');
        }
        if (!$procData['fileId']){
            showError('您还未上传结算凭证', printUrl('SingleSettle', 'StoreUpload', array('procId'=>$procId)));
        }
        $this->load->model('sellpointModel');
        $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($procData['storeId']);;
        //百县项目列表
        $this->load->model('sProjectModel');
        $this->viewData['projectList'] = $this->sProjectModel->getNewList(array('procId'=>$procId,'isBubao'=>0), NULL, NULL, '', 'id desc');
        //操作
        $this->viewData['action'] = 'edit';
        $this->load->view('SingleSettle/storeEdit', $this->viewData);
    }
    /**
     * 管理端列表
     */
    function adminList(){
        if ($this->user['userRole'] == 1){
            showError('您无权限进去此页面');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleBudget', 'adminList'),'title'=>'结算申请管理');
        //载入模型
        $this->load->model('singleProcessModel');
        $this->load->model('sellpointModel');
        //查询条件
        $where = array();
        //专营店筛选
        $area = $this->input->get('area');
        $this->viewData['area'] = $area;
        $region = $this->input->get('region');
        $this->viewData['region'] = $region;
        $province = $this->input->get('province');
        $this->viewData['province'] = $province;
        $city = $this->input->get('city');
        $this->viewData['city'] = $city;
        $storeId = $this->input->get('storeId');
        $this->viewData['storeId'] = $storeId;
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($area, $region, '', $province, $city, $storeId, $keyword);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $where['storeId'] = array('in', $placeStoreIds);
        }else{
            $where['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //系统配置
        //时间单位
        $this->viewData['timeUnit'] = (int)$this->input->get('timeUnit');
        $this->viewData['timeUnit'] && $where['timeUnit'] = $this->viewData['timeUnit'];
        //年份条件
        $where['year'] = $year;
        $where['settleApply > '] =0;
        $count = $this->singleProcessModel->getCount($where);
        //分页
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['c'], $getParam['m'], $getParam['page']);
        $config['base_url'] = printUrl('SingleSettle', 'adminList', $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = $this->input->get('page');
        $limit = $this->pagination->per_page;
        
        $procList = $this->singleProcessModel->getNewList($where, $limit, $page, '', 'id desc');
        foreach ($procList as $k => $v){
            $procList[$k]['auditCount'] = $this->singleProcessModel->getSettleStatusCount($v['id'], 0);
        }
        $this->viewData['procList'] = $procList;
        $this->load->view('SingleSettle/adminList', $this->viewData);
    }
    /**
     * 结算审核
     */
    function adminAudit(){
        if ($_POST){
            $procId = (int)$this->input->post('procId');
            $projectData = $this->input->post('project');
            $projectData || showError('您未选择审核项');
            $this->load->model('sProjectModel');
            foreach ($projectData as $k => $v){
                $this->sProjectModel->settleOneKeyAudit((int)$k, (int)$v['settleAuditRs'], $this->user);
            }
            //插入审核日志
            $this->load->model('sAuditLog');
            $this->sAuditLog->addLog(3, array(
                'targetId'=>$procId,
                'userRole'=>$this->user['userRole'],
                'userId' => $this->user['userId'],
                'auditRs' => 1,
                'auditDes' => '结算一键审核'
            ));
            showSuccess('审核成功');
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择要审核的双月数据');
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleSettle', 'adminList'),'title'=>'结算管理列表');
            $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'结算审核');
            //主线流程数据
            $this->load->model('singleProcessModel');
            $procData = $this->singleProcessModel->getNewData(array('id'=>$procId));
            $procData || showError('双月数据不存在');
            $this->viewData['procData'] = $procData;
            if (!$this->auth->isSingleSettleAudit($this->viewData['procData'])){
                showError('您无权限审核');
            }
            $this->load->model('sellpointModel');
            $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($procData['storeId']);;
            //百县项目列表
            $this->load->model('sProjectModel');
            $this->viewData['projectList'] = $this->sProjectModel->getNewList(array('procId'=>$procId,'isBubao'=>0,'settleSubTime >'=>0), NULL, NULL, '', 'id desc');
            $this->viewData['action'] = 'audit';
            $this->load->view('SingleSettle/adminAudit', $this->viewData);
        }
    }
    /**
     * 修改金额
     */
    function adminChange(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择要审核的双月数据');
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleSettle', 'adminList'),'title'=>'结算管理列表');
        $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'结算审核');
        //主线流程数据
        $this->load->model('singleProcessModel');
        $procData = $this->singleProcessModel->getNewData(array('id'=>$procId));
        $procData || showError('双月数据不存在');
        $this->viewData['procData'] = $procData;
        if (!$this->auth->isSingleSettleChange($this->viewData['procData'])){
            showError('您无权限修改金额');
        }
        $this->load->model('sellpointModel');
        $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($procData['storeId']);;
        //百县项目列表
        $this->load->model('sProjectModel');
        $this->viewData['projectList'] = $this->sProjectModel->getNewList(array('procId'=>$procId,'isBubao'=>0,'settleSubTime >'=>0), NULL, NULL, '', 'id desc');
        $this->viewData['action'] = 'change';
        $this->load->view('SingleSettle/check', $this->viewData);
    }
    /**
     * 结算查看
     */
    function check(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择要查看的双月数据');
        //面包屑
        if ($this->user['userRole'] == 1){
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleSettle', 'storeList'),'title'=>'结算申请列表');
        }else{
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SingleSettle', 'adminList'),'title'=>'结算管理列表');
        }
        $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'结算查看');
        //主线流程数据
        $this->load->model('singleProcessModel');
        $procData = $this->singleProcessModel->getNewData($procId);
        $this->viewData['procData'] = $procData;
        if (!$this->auth->isSingleBudgetCheck($this->viewData['procData'])){
            showError('您无权限查看');
        }
        $this->load->model('sellpointModel');
        $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($procData['storeId']);;
        //百县项目列表
        $this->load->model('sProjectModel');
        $this->viewData['projectList'] = $this->sProjectModel->getNewList(array('procId'=>$procId,'isBubao'=>0), NULL, NULL, '', 'id desc');
        //查看标记
        $this->viewData['action'] = 'check';
        $this->load->view('SingleSettle/check', $this->viewData);
    }
}